<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="ajax.jsp"%>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>欢迎使用后台管理系统</title>
    <style>
        /* 重置样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /* 页面主体 */
        body {
            font-family: sans-serif;
            background-color: #f5f5f5;
        }

        /* 表格 */
        table {
            width: 100%;
            border-collapse: collapse;
            border: 1px solid #ddd;
        }

        .form-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 5px;
        }

        th {
            text-align: center;
            background-color: #f2f2f2;
        }

        /* 操作按钮 */
        .button {
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }

        .button-delete {
            padding: 5px 10px;
            background-color: #f44336;
            color: white;
            border: none;
            border-radius: 3px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }

        a{
            cursor: pointer;
        }
    </style>
</head>
<body>
<script>
    function deleteCate(url){
        axiosInstance.post(
            url,
            {
                headers: headersJson
            }
        ).then((res)=>{
            console.log(res.data);
            if (res.data.code == 200){
                console.log("删除成功");
                document.getElementById('msg').innerHTML = "删除成功";
                setTimeout(()=>{
                    document.getElementById('msg').innerHTML = "";
                    location.reload();
                }, 2000);
            }else {
                console.log("删除失败");
                document.getElementById('msg').innerHTML = "删除失败";
                setTimeout(()=>{
                    document.getElementById('msg').innerHTML = "";
                }, 2000);
            }
        }).catch(e=>{
            console.log(e);
            console.log("删除出现异常");
            document.getElementById('msg').innerHTML = "删除出现异常";
            setTimeout(()=>{
                document.getElementById('msg').innerHTML = "";
            }, 2000);
        })
    }
</script>
<div id="msg"></div>
<div class="main">
    <div class="flex-row-between" style="justify-content: space-between; margin: 0 10px">
        <h2 style="margin-top: 10px">商品类型数据列表</h2>
        <button class="button-blue" onclick="navigateTo('/cate/createCate')">增加类型</button>
    </div>
    <div style="margin-top: 20px">
        <table class="table">
            <thead>
            <tr>
                <th style="text-align: center" width="300">类型名称</th>
                <th style="text-align: center" width="300">备注</th>
                <th style="text-align: center" width="150">操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${cateList}" var="cate">
                <tr align="center">
                    <td>${cate.cateName}</td>
                    <td>${cate.memo}</td>
                    <td>
                        <a class="button" onclick="navigateTo('/cate/getCateById/${cate.cateId}')">编辑</a>&nbsp;&nbsp;
                        <a class="button-delete"
                           onclick="{if(confirm('确定要删除吗?')){deleteCate('/cate/deleteCate/${cate.cateId}');}return false;}">删除</a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
